package com.basic.url;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Description:
 * @Date: 2024-03-28 09:30
 * @Author: wz
 **/
public class UrlTest {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://img-blog.csdnimg.cn/20210616183408630.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMzU4NTc0,size_16,color_FFFFFF,t_70");
        System.out.println(url.getPort());
        System.out.println(url.getPath());
        System.out.println(url.getQuery());
        System.out.println(url.getHost());
        System.out.println(url.getAuthority());
        System.out.println(url.getContent());

        URLConnection urlConnection = url.openConnection();
        urlConnection.connect();

        InputStream inputStream = url.openStream();
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Mr.Wang\\Desktop\\t.png");
        byte[] bytes = new byte[1024];
        int len;
        while ((len = inputStream.read(bytes)) != -1){
            fos.write(bytes,0,len);
        }
        inputStream.close();
        fos.close();
    }
}
